<%
'******************************************************************************************
'邮件发送类MailSender
'本类中共提供3种邮件发送组件
'Publisher:WangJinkun.Date:2011-07-01 
'Copyright (C) evil_god@163.com (http://blog.163.com/evil_god).All rights reserved.
'******************************************************************************************
Class MailSender
	public Mailhostserver_con,MailUser_con,MailPwd_con,MailSMTP_con,MailPort_con,MailTLS_con,MailSendApp_con
	
	Public Sub Class_Initialize() '类的初始化.通常,可以在这设置一些默认值
		Mailhostserver_con = ""
		MailUser_con = ""
		MailPwd_con = ""
		MailSMTP_con = ""
		MailPort_con = ""
		MailTLS_con = ""
		MailSendApp_con = ""
	End Sub 
	'函数名：mail
	'作  用：邮件发送入口函数
	'参  数：FName：发件者名称，ToWho：邮件接收者（多个使用英文格式";"），subject：邮件主题，mailbody：邮件正文，pic：附件
	public function mail(ByVal FName, ByVal ToWho, ByVal subject, ByVal mailbody, ByVal pic)
		if Mailhostserver_con<>"" and MailUser_con<>""  and MailPwd_con<>""  and MailSMTP_con<>""  and MailSendApp_con<>"" then
			select case MailSendApp_con
				case 1
					mail=SendJMail(FName,ToWho,subject,mailbody,pic)
				case 2
					mail=SendAspEmail(FName,ToWho,subject,mailbody,pic)
				case 3
					mail=SendCdoMail(FName,ToWho,subject,mailbody,pic)
				case else
					mail=False &": Please select the corresponding application components."
			end select
		else
			mail=False
		end if
	end function
	
	'Jmail
	private function SendJMail(ByVal FName, ByVal ToWho, ByVal subject, ByVal mailbody, ByVal pic)
		On error resume next
		Dim JMail
		Set JMail = Server.CreateObject("JMail.Message") 
		JMail.Charset = "utf-8" ' 邮件字符集，默认为"US-ASCII"
		'JMail.ISOEncodeHeaders = True ' 是否进行ISO编码，默认为True
		JMail.From = Mailhostserver_con ' 发送者地址
		JMail.FromName = FName ' 发送者姓名
		JMail.Subject = subject ' 邮件主题
		JMail.MailServerUserName = MailUser_con ' 身份验证的用户名
		JMail.MailServerPassword = MailPwd_con ' 身份验证的密码
		JMail.Priority = 3
		JMail.AddHeader "Originating-IP", Request.ServerVariables("REMOTE_ADDR")
		if instr(ToWho,";")<=0 then
			JMail.AddRecipient(ToWho)
		else
			arrToWho=split(ToWho,";")
			for arrToWho_i=0 to ubound(arrToWho)
				JMail.AddRecipient(arrToWho(arrToWho_i))
			next
		end if
		if pic<>"" then
			JMail.AddAttachment Server.MapPath(pic),True ' 【参数设置是(True)否(False)为Inline方式】
		end if
		' 邮件主体（HTML(注意信件内链接附件的方式)）
		JMail.HTMLBody = "<html><head><META content=zh-cn http-equiv=Content-Language><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""><style type=text/css>A:link { FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #000000}A:visited {FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #666666}A:hover {COLOR: #ff6600; FONT-SIZE: 9pt; TEXT-DECORATION: underline}BODY {FONT-SIZE: 9pt}td {FONT-SIZE: 9pt} --></style></head><body><p>"&mailbody&"</p></body></html>"
		' 邮件主体（文本部分）
		JMail.Body = "我们的邮件采用了HTML格式，但是您的邮件查看软件可能不支持。。。邮件正文,邮件正文,邮件正文"
		' 发送【调用格式：objJMail.Send([username:password@]SMTPServerAddress[:Port])】
		if MailPort_con<>25 then MailSMTP_con=MailSMTP_con&":"&MailPort_con
		JMail.Send(MailSMTP_con)
		JMail.Close()
		Set JMail = Nothing
		if err.number<>0 then
			SendJMail=False&": Jmail - "&err.description
		else
			SendJMail=True
		end if
	end function
	
	'AspEmail
	private function SendAspEmail(ByVal FName, ByVal ToWho, ByVal subject, ByVal mailbody, ByVal pic)
		On error resume next
		Dim Mail
		Set Mail = Server.CreateObject("Persits.MailSender")
		Mail.CharSet = "UTF-8"
		Mail.ContentTransferEncoding = "Quoted-Printable"
		Mail.Host = MailSMTP_con ' enter valid SMTP host
		Mail.Username = MailUser_con
		Mail.Password = MailPwd_con
		if MailTLS_con=True then Mail.TLS = True
		Mail.Port = MailPort_con
		Mail.From = Mailhostserver_con ' From address
		Mail.FromName = Mail.EncodeHeader(FName, "utf-8") ' optional
		if instr(ToWho,";")<=0 then
			Mail.AddAddress ToWho
		else
			arrToWho=split(ToWho,";")
			for arrToWho_i=0 to ubound(arrToWho)
				Mail.AddAddress arrToWho(arrToWho_i)
			next
		end if
		' message subject
		Mail.Subject = Mail.EncodeHeader(subject, "utf-8")
		' message body
		Mail.IsHTML = True
		Mail.Body = "<html><head><META content=zh-cn http-equiv=Content-Language><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""><style type=text/css>A:link { FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #000000}A:visited {FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #666666}A:hover {COLOR: #ff6600; FONT-SIZE: 9pt; TEXT-DECORATION: underline}BODY {FONT-SIZE: 9pt}td {FONT-SIZE: 9pt} --></style></head><body><p>"&mailbody&"</p></body></html>"
		if pic<>"" then Mail.AddAttachment Server.MapPath(pic)
		Mail.Send ' send message
		If Err <> 0 Then ' error occurred
			SendAspEmail = False&": AspEmail - "&err.description
		else
			SendAspEmail = True
		End If
		Set JMail = Nothing
	end function

	'Cdo.Message
	private function SendCdoMail(ByVal FName, ByVal ToWho, ByVal subject, ByVal mailbody, ByVal pic)
		On Error Resume Next
		Dim sch,cdoConfig,cdoMessage
		sch = "http://schemas.microsoft.com/cdo/configuration/"
		Set cdoConfig = Server.CreateObject("CDO.Configuration")
		With cdoConfig.Fields 
		.Item(sch & "smtpserver") = MailSMTP_con
		.Item(sch & "smtpserverport") = MailPort_con
		.Item(sch & "sendusing") = 2
		.Item(sch & "smtpaccountname") = FName
		.Item(sch & "sendemailaddress") = Mailhostserver_con
		.Item(sch & "smtpuserreplyemailaddress") = 25
		.Item(sch & "smtpauthenticate") = 1
		.Item(sch & "sendusername") = MailUser_con
		.Item(sch & "sendpassword") = MailPwd_con
		.update 
		End With
		If Err<>0 Then
			SendCdoMail=False &": CDO - "&err.description
		else
			Set cdoMessage = Server.CreateObject("CDO.Message") 
			Set cdoMessage.Configuration = cdoConfig 
			With cdoMessage
			.BodyPart.Charset = "utf-8" 
			.To = ToWho  
			.From = Mailhostserver_con
			.Subject = subject
			.htmlBody = mailbody
			.Send 
			End With
			If Err<>0 Then
				SendCdoMail=False &": CDO - "&err.description
			Else
				SendCdoMail=True
			End If
			Set cdoMessage = Nothing 
		End If
		Set cdoConfig = Nothing
	end function
	
	Public Sub Class_Terminate() '类结束时触发本事件.
		Mailhostserver_con = ""
		MailUser_con = ""
		MailPwd_con = ""
		MailSMTP_con = ""
		MailPort_con = ""
		MailTLS_con = ""
		MailSendApp_con = ""
	End Sub 
End Class
'******************************************************************************************
'Copyright (C) evil_god@163.com (http://blog.163.com/evil_god).All rights reserved.
'******************************************************************************************
%>